/**
 * 项目project.js
 * 版本号： v1.0.0
 */
$(function () {
    var lastChecked = null;
    $("#project_dataList").datagrid({
        fitColumns: true,
        fit: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/project/projectList.do',
        idField: 'prId',
        frozenColumns: [[
            {field: '', checkbox: true}
        ]],
        columns: [
            [
                {field: 'prName', title: '项目名称', width: 260, fixed: true},
                {field: 'prType',title: '项目类型', width: 70, fixed: true, formatter:function(val){
                        if(val) {
                            return val.propertyName;
                        }
                    }},
                {field: 'prLocal', title: '项目标段', width: 260},
                {field: 'prLeader', title: '项目负责人', width: 90, fixed: true},
                {field: 'prRealStart', title: '实际开始日期', width: 100, fixed: true, formatter:function(val){
                        return formatCalendar(val);
                    }},
                {field: 'prRealEnd', title: '实际结束日期', width: 100, fixed: true, formatter:function(val){
                        return formatCalendar(val);
                    }}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#project_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#project_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#project_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#project_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            window.location.href = CONTEXT_PATH + "/project/toProject.do";
                        }
                    });
                }
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            project_del();
                        }
                    });
                }
                //修改按钮
                if("editBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            project_edit();
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            //加载所属公司下拉列表
                            project_loadCompany();
                            project_findList();
                        }
                    });
                }
                //查看详细按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            project_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 删除项目
 */
function project_del() {
    var row = $("#project_dataList").datagrid("getSelected");
    var checked = $("#project_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({title: '操作提示', msg: "请选择删除项目", timeout: 3000, showType: "slide"});
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.prName + ' 项目吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/project/del.do?prId=" + row.prId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#project_dataList").datagrid("reload", {
                        prName:$("#prName_").val(),
                        prType:$("#prType_").combobox("getValue")
                    });
                }
            })
        }
    });
}

/**
 * 修改项目
 * @returns {boolean}
 */
function project_edit() {
    var row = $("#project_dataList").datagrid("getSelected");
    var checked = $("#project_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({title: '操作提示', msg: "请选择修改项目", timeout: 3000, showType: "slide"});
        return;
    }
    window.location.href = CONTEXT_PATH + "/project/toProject.do?prId=" + row.prId;
}

//查询项目
function project_findList() {
    refreshPageNumber("project_dataList");
    $("#project_dataList").datagrid("reload",{
        prName:$("#prName_").val(),
        prType:$("#prType_").combobox("getValue")
    });
    //清楚选中项
    $("#project_dataList").datagrid("clearSelections");
}

/**
 * 加载所有的公司下拉
 */
function project_loadCompany() {
    //所属公司下拉树
    $('#companyTree').combotree({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        onSelect:function(node){
            $('#companyId').val(node.id);
        }
    });

}

/***
 * 动态刷新页面
 * @param prId
 */
function refreshPageNumber(csId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + csId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + csId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}

/**
 * 查看项目详情
 */
function project_view(iconCls){
    var row = $("#project_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的项目详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(xmxq.id,xmxq.url + row.prId+"&showNewWindow=false", iconCls, xmxq.closable,xmxq.title);
}
